#include "timerdialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QDebug>


TimerDialog::TimerDialog():m_counter1(0), m_counter2(0)
{
    m_label = new QLabel("timer1: 0", this);
    m_label2 = new QLabel("timer2: 0", this);
    m_setButton = new QPushButton("set", this);
    m_stopButton = new QPushButton("stop", this);
    m_timerValue = new QLineEdit("300", this);
    m_comboBox = new QComboBox(this);
    m_comboBox -> addItem("timer1", m_timerId1);
    m_comboBox -> addItem("timer2", m_timerId2);

    setLayout(new QVBoxLayout);

    QHBoxLayout* hl = new QHBoxLayout();

    layout() -> addWidget(m_label);
    layout() -> addWidget(m_label2);
    layout() -> addWidget(m_comboBox);
    hl ->  addWidget(m_timerValue);
    hl -> addWidget(m_setButton);
    layout() -> addItem(hl);
    layout() -> addWidget(m_stopButton);

    connect(m_setButton, SIGNAL(clicked()), this, SLOT(setTimer()));
    connect(m_stopButton, SIGNAL(clicked()), this, SLOT(stopTimer()));
    //connect(m_comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(setTimer()));
}

void TimerDialog::timerEvent(QTimerEvent *te)
{
    if(m_timerId1 == te->timerId())
    {
        m_counter1++;
        m_label -> setText("timer1: " + QString::number(m_counter1));
    }
    else if(m_timerId2 == te->timerId())
    {
        m_counter2++;
        m_label2 -> setText("timer2: " + QString::number(m_counter2));
    }
}

void TimerDialog::setTimer()
{
    if(m_comboBox->currentIndex() == 0)
    {
        killTimer(m_timerId1);
        m_timerId1 = startTimer(m_timerValue->text().toInt());
    }
    else if(m_comboBox->currentIndex() == 1)
    {
        killTimer(m_timerId2);
        m_timerId2 = startTimer(m_timerValue->text().toInt());
    }
}

void TimerDialog::stopTimer()
{
    if(m_comboBox->currentIndex() == 0)
        killTimer(m_timerId1);
    else if(m_comboBox->currentIndex() == 1)
        killTimer(m_timerId2);
}
